$(function(){
    let form = layui.form
    //1,表单的正则
    form.verify({
        nickname:[
            /^[\S]{1,10}$/,
            '昵称的长度位1-10个字符'
        ]
    })

    //2,渲染用户信息
    initUserInfo()

    function initUserInfo () {
        axios({
            url:'/my/userinfo',
            method:'get',
           
        }).then(({data:res})=>{
            //成功回调
            console.log(res)
            if(res.status != 0 ) return layer.msg(res.message)
            form.val('formUserInfo',res.data)
        }); 
    }

    //3重置功能
    $('#btnReset').on('click',function(e){
        e.preventDefault()
        initUserInfo()
    })

    //4，提交更新
    $('form').on('submit',function(e){
        e.preventDefault()
        axios({
            url:'/my/userinfo',
            method:'post',
            data: $(this).serialize()
        }).then(({data:res})=>{
            if(res.status != 0 ) return layer.msg(res.message)
            layer.msg("恭喜您！修改成功！")
            window.parent.getUserInfo()
        });
    })
})